package com.jwpdk.dbframework;

import java.sql.Connection;
import java.util.Hashtable;

import org.apache.log4j.Logger;
/**
 * 
 * @author 王志伟
 *
 */
public class ConnectionManager {

	private final static Logger						log				= Logger.getLogger(ConnectionManager.class);
	/**
	 * 当前连接
	 */
	private static ThreadLocal< ConnectionManager>	tradeLocal		= new ThreadLocal< ConnectionManager>();
	private Hashtable< String, Connection>			connectionPools	= new Hashtable< String, Connection>();

	/**
	 * get instance
	 * @return ConnectionManager
	 * @throws Exception
	 */
	public static ConnectionManager getInstance() throws Exception {
		ConnectionManager instance = tradeLocal.get();
		if ( instance == null ) {
			instance = new ConnectionManager();
			tradeLocal.set(instance);
		}
		return instance;
	}

	/**
	 * 向池中新增连接
	 * @param transactionName
	 * @param connection
	 */
	public synchronized void addConnectionToPool(String transactionName) throws Exception {
		if ( connectionPools.containsKey(transactionName) ) {
			String errString = "【" + transactionName + "】此连接已存在:" + connectionPools.get(transactionName);
			log.debug(errString);
			throw new Exception(errString);
		} else {
			connectionPools.put(transactionName,getNewConnection());
		}
	}

	/**
	 * 指定连接名称
	 * @param transactionName
	 * @param connection
	 */
	public synchronized void setConnectionPool(String transactionName, Connection connection) {
		connectionPools.put(transactionName,connection);

	}

	/**
	 * 根据连接名从池中移除
	 * @param transactionName
	 */
	public Connection removeConnection(String transactionName) {
		return connectionPools.remove(transactionName);
	}

	/**
	 * 根据连接名获取连接
	 * @param transactionName
	 * @return
	 */
	public Connection getConnection(String transactionName) throws Exception {
		Connection conn = connectionPools.get(transactionName);
		if ( conn == null || conn.isClosed() ) {
			connectionPools.remove(transactionName);
			conn = null;
		}
		return conn;
	}

	/**
	 * 获取新连接
	 * @return
	 * @throws Exception
	 */
	public Connection getNewConnection() throws Exception {
		Connection conn = DBFactory.getInstance().getConnection();
		if ( log.isDebugEnabled() ) {
			log.debug("获取新连接:" + conn.toString());
		}
		return conn;
	}

	/**
	 * 判断当前是否不存在连接。
	 * @return 不存在 true 存在 false
	 */
	public boolean isNotExistsConnection(String transactionName) {
		boolean flag = true;
		try {
			Connection conn = getConnection(transactionName);
			if ( conn != null && !conn.isClosed() )
				flag = false;
		} catch ( Exception e ) {
		}
		return flag;
	}

	/**
	 * 关闭数据库连接
	 * @throws Exception
	 */
	public static void closeConnection(String transactionName) throws Exception {
		Connection conn = getInstance().removeConnection(transactionName);
		if ( conn != null && !conn.isClosed() ) {
			log.debug("关闭连接：" + conn);
			conn.close();
		}
		conn = null;
	}

	/**
	 * 回滚数据库连接
	 * @throws Exception
	 */
	public static void rollbackConnection(String transactionName) throws Exception {
		Connection conn = getInstance().getConnection(transactionName);
		if ( conn != null && !conn.isClosed() )
			conn.rollback();
	}

	/**
	 * 提交数据库连接
	 * @throws Exception
	 */
	public static void commitConnection(String transactionName) throws Exception {
		Connection conn = getInstance().getConnection(transactionName);
		if ( conn != null && !conn.isClosed() )
			conn.commit();
	}
}